package com.xiaochong.bitong.api.controller;

import com.netflix.discovery.converters.Auto;
import com.xc.logclient.utils.LogTrace;
import com.xiaochong.bitong.api.config.H5Url;
import com.xiaochong.bitong.api.constant.ReturnCode;
import com.xiaochong.bitong.api.service.CurrencyService;
import com.xiaochong.bitong.api.service.ReportService;
import com.xiaochong.bitong.api.utils.ApiVersion;
import com.xiaochong.bitong.api.utils.StringUtil;
import com.xiaochong.service.common.config.oss.OSSTool;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.common.data.mapper.BannerMapper;
import com.xiaochong.tir.common.data.mapper.ProgramMapper;
import com.xiaochong.tir.common.data.mapper.SummaryMapper;
import com.xiaochong.tir.common.data.po.*;
import com.xiaochong.tir.common.data.vo.ApiResultVo;
import com.xiaochong.tir.common.data.vo.BannerVo;
import com.xiaochong.tir.common.data.vo.CurrencyQuotationResultVo;
import com.xiaochong.tir.common.data.vo.CurrencyQuotationVo;
import com.xiaochong.tir.common.util.ApiResultCodeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Iterator;
import java.util.List;

/**
 * 币种
 * @author MengXiangDong
 */
@RestController
@Api(value = "研报")
@RequestMapping(value = ApiVersion.API_V1_PREFIX+"/report")
public class ReportController {

	@Autowired
	private RedisServer redisServer;

	@Autowired
	private ReportService reportService;

	@Autowired
	private BannerMapper bannerMapper;

	@Autowired
	private OSSTool ossTool;

	@Autowired
	private ProgramMapper programMapper;

	@Autowired
	private SummaryMapper summaryMapper;

	/**
	 * 成交榜
	 */
	@ApiOperation(value = "研报列表" , notes = "研报列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "pageIndex", value = "页数", required = true, dataType = "Integer", paramType = "query"),
			@ApiImplicitParam(name = "pageSize", value = "条数", required = true, dataType = "Integer", paramType = "query")
	})
	@PostMapping("reportList")
	public ApiResultVo<List<SyResearchReport>> reportList(@RequestParam(value = "pageIndex", required = true) Integer pageIndex,
														  @RequestParam(value = "pageSize", required = true) Integer pageSize
																 ){
		CurrencyQuotationVo currencyQuotationVo = new CurrencyQuotationVo();
		currencyQuotationVo.setPageIndex( pageIndex );
		currencyQuotationVo.setPageSize( pageSize );
		ApiResultVo apiResultVo=reportService.reportList(currencyQuotationVo);
		return apiResultVo;
	}

	/**
	 * 研报详情
	 */
	@ApiOperation(value = "研报详情" , notes = "研报详情（参数id）")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Integer", paramType = "query")
	})
	@PostMapping("reportDetails")
	public ApiResultVo<List<SyRotationMap>> reportDetails(@RequestParam(value = "id", required = true) Integer id) {
		SyResearchReport syResearchReport = new SyResearchReport();
		syResearchReport.setId( id );
		ApiResultVo api=reportService.reportDetails(syResearchReport);
		return api;
	}

	/**
	 * 成交榜
	 */
	@ApiOperation(value = "banner列表" , notes = "banner列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "pageIndex", value = "页数", required = true, dataType = "Integer", paramType = "query"),
			@ApiImplicitParam(name = "pageSize", value = "条数", required = true, dataType = "Integer", paramType = "query")
	})
	@PostMapping("bannerList")
	public ApiResultVo<List<Banner>> bannerList(@RequestParam(value = "pageIndex", required = true) Integer pageIndex,
												@RequestParam(value = "pageSize", required = true) Integer pageSize){
		ApiResultVo apiResultVo=new ApiResultVo();
		apiResultVo.setTradeId( LogTrace.getTrace().getTraceId() );
		try {
			Banner banner = new Banner();
			banner.setEnableFlag( true );
			List<Banner> bannerList=bannerMapper.selectBannerListBySort( banner );
			for (int i=0;i<bannerList.size();i++){
				 banner = bannerList.get( i );
				if(StringUtils.isNotBlank( banner.getBannerImg() ) && !banner.getBannerImg().contains( "http" )){
					String imgUrl=ossTool.getOSSUrl( banner.getBannerImg() );
					banner.setBannerImg( imgUrl );
				}
				//native
				if(StringUtils.isNotBlank( banner.getPublishMode() ) && StringUtils.isNotBlank( banner.getRedirectUrl() )){
					if(banner.getPublishMode().equals( "2" ) && !banner.getRedirectUrl().contains( "http" )){
						banner.setRedirectUrl( H5Url.getReport() +banner.getRedirectUrl() );
					}
				}
			}
			apiResultVo.setCode( ReturnCode.SUCCESS);

			//分页
			if(pageIndex==null){
				pageIndex=1;
			}
			if(pageSize==null){
				pageSize=5;
			}
			Object result= StringUtil.pageBean(bannerList,pageIndex,pageSize );

			apiResultVo.setData( result );
			apiResultVo.setTotal( bannerList.size() );
		}catch (Exception e){
			e.printStackTrace();
			apiResultVo.setCode( ReturnCode.ERROR );
			LogTrace.error("banner列表",e);
		}
		return apiResultVo;
	}


	/**
	 * 栏目列表
	 */
	@ApiOperation(value = "栏目列表" , notes = "栏目列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "pageIndex", value = "页数", required = true, dataType = "Integer", paramType = "query"),
			@ApiImplicitParam(name = "pageSize", value = "条数", required = true, dataType = "Integer", paramType = "query")
	})
	@PostMapping("programList")
	public ApiResultVo<List<Banner>> programList(@RequestParam(value = "pageIndex", required = true) Integer pageIndex,
												 @RequestParam(value = "pageSize", required = true) Integer pageSize){
		ApiResultVo apiResultVo=new ApiResultVo();
		apiResultVo.setTradeId( LogTrace.getTrace().getTraceId() );
		try {
			Program program = new Program();
			program.setEnableFlag( true );
			List<Program> ProgramList=programMapper.selectByEntityList( program );
			for (int i=0;i<ProgramList.size();i++){
				program = ProgramList.get( i );
				if(StringUtils.isNotBlank( program.getColumnImg() )){
					String imgUrl=ossTool.getOSSUrl( program.getColumnImg());
					if(StringUtils.isNotBlank( imgUrl )){
						program.setColumnImg( imgUrl );
					}
				}
			}
			apiResultVo.setCode( ReturnCode.SUCCESS);

			//分页
			if(pageIndex==null){
				pageIndex=1;
			}
			if(pageSize==null){
				pageSize=4;
			}

			Object result= StringUtil.pageBean(ProgramList,pageIndex,pageSize );
			apiResultVo.setData( result );
			apiResultVo.setTotal( ProgramList.size() );
		}catch (Exception e){
			e.printStackTrace();
			apiResultVo.setCode( ReturnCode.ERROR );
			LogTrace.error("banner列表",e);
		}
		return apiResultVo;
	}

	/**
	 * 简介
	 */
	@ApiOperation(value = "简介" , notes = "简介")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "type", value = "类型（1：大盘，2：权重，3：行业，4概念）", required = true, dataType = "Integer", paramType = "query"),
			@ApiImplicitParam(name = "id", value = "概念或者行业id", required = false, dataType = "Integer", paramType = "query")
	})
	@PostMapping("summary")
	public ApiResultVo<List<Banner>> summary(@RequestParam(value = "type", required = true) Integer type,
											 @RequestParam(value = "id", required = false) Integer id){
		ApiResultVo apiResultVo=new ApiResultVo();
		apiResultVo.setTradeId( LogTrace.getTrace().getTraceId() );
		try {
			Summary summary = new Summary();
			summary.setType( String.valueOf( type ) );
			if(type==3||type==4){
				summary.setRelationId( id );
			}
			List<Summary> summaryList=summaryMapper.selectByEntityList( summary );
			if(summaryList.size()>0){
				apiResultVo.setData( summaryList.get( 0 ) );
				apiResultVo.setCode( ReturnCode.SUCCESS );
			}else{
				apiResultVo.setCode( ReturnCode.DATA_NOT_EXIST );
			}
		}catch (Exception e){
			e.printStackTrace();
			apiResultVo.setCode( ReturnCode.ERROR );
			LogTrace.error("简介",e);
		}
		return apiResultVo;
	}




}
